package com.androidx.p013lv.mine.fragment;

import android.os.Bundle;
import android.view.View;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.androidx.p013lv.base.bean.ReqCollect;
import com.androidx.p013lv.base.bean.VideoBean;
import com.androidx.p013lv.base.bean.event.LikeVideoEvent;
import com.androidx.p013lv.base.http.BaseRes;
import com.androidx.p013lv.base.listener.ItemClickListener;
import com.androidx.p013lv.base.p015ui.LazyFragment;
import com.androidx.p013lv.base.utils.ToastUtils;
import com.androidx.p013lv.base.utils.UiUtils;
import com.androidx.p013lv.base.view.StatusControlLayout;
import com.androidx.p013lv.mine.C1207R;
import com.androidx.p013lv.mine.adapter.MineCollectVideoAdapter;
import com.androidx.p013lv.mine.bean.CollectVideoBean;
import com.androidx.p013lv.mine.databinding.FragmentCollectBinding;
import com.androidx.p013lv.mine.model.MineCollectModel;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.yanzhenjie.recyclerview.OnItemMenuClickListener;
import com.yanzhenjie.recyclerview.SwipeMenu;
import com.yanzhenjie.recyclerview.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.SwipeMenuItem;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;
import java.util.Iterator;
import java.util.List;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/* renamed from: com.androidx.lv.mine.fragment.CollectFragment */
public class CollectFragment extends LazyFragment<FragmentCollectBinding> implements OnRefreshLoadMoreListener, ItemClickListener {
    private MineCollectVideoAdapter adapter;
    private RecyclerView.AdapterDataObserver dataObserver = new RecyclerView.AdapterDataObserver() {
        /* class com.androidx.p013lv.mine.fragment.CollectFragment.C13091 */

        @Override // androidx.recyclerview.widget.RecyclerView.AdapterDataObserver
        public void onChanged() {
            CollectFragment.this.deleteChangeState();
        }

        @Override // androidx.recyclerview.widget.RecyclerView.AdapterDataObserver
        public void onItemRangeChanged(int i, int i2) {
            CollectFragment.this.deleteChangeState();
        }
    };
    private boolean isEdit;
    private OnItemMenuClickListener mItemMenuClickListener = new OnItemMenuClickListener() {
        /* class com.androidx.p013lv.mine.fragment.CollectFragment.C13146 */

        @Override // com.yanzhenjie.recyclerview.OnItemMenuClickListener
        public void onItemClick(SwipeMenuBridge swipeMenuBridge, int i) {
            swipeMenuBridge.closeMenu();
            CollectFragment.this.deleteSingleCollect(i);
        }
    };
    private SwipeMenuCreator mSwipeMenuCreator = new SwipeMenuCreator() {
        /* class com.androidx.p013lv.mine.fragment.CollectFragment.C13135 */

        @Override // com.yanzhenjie.recyclerview.SwipeMenuCreator
        public void onCreateMenu(SwipeMenu swipeMenu, SwipeMenu swipeMenu2, int i) {
            SwipeMenuItem swipeMenuItem = new SwipeMenuItem(CollectFragment.this.getActivity());
            swipeMenuItem.setBackgroundColorResource(C1207R.C1208color.mainColor);
            swipeMenuItem.setWidth(UiUtils.dp2px(70));
            swipeMenuItem.setHeight(-1);
            swipeMenuItem.setTextSize(16);
            swipeMenuItem.setText("刪除");
            swipeMenuItem.setTextColorResource(C1207R.C1208color.white);
            swipeMenu2.setOrientation(1);
            swipeMenu2.addMenuItem(swipeMenuItem);
        }
    };
    private MineCollectModel model;
    private int page = 1;

    public static CollectFragment newInstance() {
        Bundle bundle = new Bundle();
        CollectFragment collectFragment = new CollectFragment();
        collectFragment.setArguments(bundle);
        return collectFragment;
    }

    @Override // com.androidx.p013lv.base.p015ui.LazyFragment
    public int setFragmentView() {
        return C1207R.C1212layout.fragment_collect;
    }

    @Override // com.androidx.p013lv.base.p015ui.LazyFragment
    public void initViews() {
        EventBus.getDefault().register(this);
        this.model = (MineCollectModel) new ViewModelProvider(getActivity()).get(MineCollectModel.class);
        ((FragmentCollectBinding) this.binding).setClick(new OnClick());
        ((FragmentCollectBinding) this.binding).setIsEdit(Boolean.valueOf(this.isEdit));
        ((FragmentCollectBinding) this.binding).setSelectNum(0);
        ((FragmentCollectBinding) this.binding).recycler.setSwipeMenuCreator(this.mSwipeMenuCreator);
        ((FragmentCollectBinding) this.binding).recycler.setOnItemMenuClickListener(this.mItemMenuClickListener);
        MineCollectVideoAdapter mineCollectVideoAdapter = new MineCollectVideoAdapter();
        this.adapter = mineCollectVideoAdapter;
        mineCollectVideoAdapter.setItemListener(this);
        ((FragmentCollectBinding) this.binding).recycler.setLayoutManager(new LinearLayoutManager(getContext()));
        ((FragmentCollectBinding) this.binding).recycler.setAdapter(this.adapter);
        this.adapter.registerAdapterDataObserver(this.dataObserver);
        ((FragmentCollectBinding) this.binding).statusLayout.setOnRetryListener(new View.OnClickListener() {
            /* class com.androidx.p013lv.mine.fragment.CollectFragment.View$OnClickListenerC13102 */

            public void onClick(View view) {
                CollectFragment.this.page = 1;
                ((FragmentCollectBinding) CollectFragment.this.binding).setStatus(2);
                CollectFragment.this.model.userCollections(CollectFragment.this.page);
            }
        });
        ((FragmentCollectBinding) this.binding).refresh.setOnRefreshListener(this);
        ((FragmentCollectBinding) this.binding).refresh.setOnLoadMoreListener(this);
        this.model.dataCollect().observe(this, new Observer<BaseRes<CollectVideoBean>>() {
            /* class com.androidx.p013lv.mine.fragment.CollectFragment.C13113 */

            public void onChanged(BaseRes<CollectVideoBean> baseRes) {
                CollectFragment.this.hideLoading();
                if (baseRes.getCode() == 200) {
                    CollectVideoBean data = baseRes.getData();
                    if (CollectFragment.this.page == 1) {
                        if (data == null || data.getList() == null || data.getList().size() == 0) {
                            ((FragmentCollectBinding) CollectFragment.this.binding).setStatus(4);
                            return;
                        }
                        List<VideoBean> list = data.getList();
                        CollectFragment.this.adapter.setDomain(data.getDomain());
                        CollectFragment.this.adapter.setData(list);
                        if (list.size() < 30) {
                            ((FragmentCollectBinding) CollectFragment.this.binding).setStatusRefresh(2);
                        }
                    } else if (data == null || data.getList() == null || data.getList().size() == 0) {
                        ((FragmentCollectBinding) CollectFragment.this.binding).setStatusRefresh(2);
                    } else {
                        List<VideoBean> list2 = data.getList();
                        CollectFragment.this.adapter.setDatasInEnd(list2);
                        if (list2.size() < 30) {
                            ((FragmentCollectBinding) CollectFragment.this.binding).setStatusRefresh(2);
                        }
                    }
                } else if (CollectFragment.this.page == 1) {
                    ((FragmentCollectBinding) CollectFragment.this.binding).setStatus(1);
                } else {
                    ToastUtils.getInstance().showWrong(baseRes.getMsg());
                }
            }
        });
        ((FragmentCollectBinding) this.binding).setStatus(2);
        this.model.userCollections(this.page);
        this.model.getActionLive().observe(this, new Observer<Boolean>() {
            /* class com.androidx.p013lv.mine.fragment.CollectFragment.C13124 */

            public void onChanged(Boolean bool) {
                CollectFragment.this.isEdit = bool.booleanValue();
                ((FragmentCollectBinding) CollectFragment.this.binding).setIsEdit(Boolean.valueOf(CollectFragment.this.isEdit));
                CollectFragment.this.editCollect();
            }
        });
    }

    @Override // com.scwang.smartrefresh.layout.listener.OnLoadMoreListener
    public void onLoadMore(RefreshLayout refreshLayout) {
        int i = this.page + 1;
        this.page = i;
        this.model.userCollections(i);
    }

    @Override // com.scwang.smartrefresh.layout.listener.OnRefreshListener
    public void onRefresh(RefreshLayout refreshLayout) {
        this.page = 1;
        this.model.userCollections(1);
    }

    @Override // com.androidx.p013lv.base.listener.ItemClickListener
    public void onItemClick(View view, int i) {
        if (!isOnClick()) {
            if (!this.isEdit) {
                startVideoPlay(((VideoBean) this.adapter.getDataInPosition(i)).getVideoId());
                return;
            }
            VideoBean videoBean = (VideoBean) this.adapter.getDataInPosition(i);
            int i2 = 1;
            if (((VideoBean) this.adapter.getDataInPosition(i)).getEditState() == 1) {
                i2 = 2;
            }
            videoBean.setEditState(i2);
            this.adapter.notifyItemChanged(i, Integer.valueOf(C1207R.C1210id.img_select));
        }
    }

    public void hideLoading() {
        ((FragmentCollectBinding) this.binding).setStatus(0);
        ((FragmentCollectBinding) this.binding).setStatusRefresh(1);
    }

    public static void showStatusView(StatusControlLayout statusControlLayout, int i) {
        statusControlLayout.showStatusView(i);
    }

    public static void refreshStatus(SmartRefreshLayout smartRefreshLayout, int i) {
        if (i == 1) {
            smartRefreshLayout.finishLoadMore();
            smartRefreshLayout.finishRefresh();
        }
        if (i == 2) {
            smartRefreshLayout.finishLoadMoreWithNoMoreData();
        }
    }

    public static void refreshEnabled(SmartRefreshLayout smartRefreshLayout, boolean z) {
        smartRefreshLayout.setEnableRefresh(!z);
        smartRefreshLayout.setEnableLoadMore(!z);
    }

    public static void isSwipeMenuList(SwipeRecyclerView swipeRecyclerView, boolean z) {
        swipeRecyclerView.setSwipeItemMenuEnabled(!z);
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void deleteChangeState() {
        if (this.isEdit) {
            int i = 0;
            for (int i2 = 0; i2 < this.adapter.getData().size(); i2++) {
                if (((VideoBean) this.adapter.getData().get(i2)).getEditState() == 2) {
                    i++;
                }
            }
            ((FragmentCollectBinding) this.binding).setSelectNum(Integer.valueOf(i));
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void deleteCollectVideo() {
        ReqCollect reqCollect = new ReqCollect();
        Iterator it = this.adapter.getData().iterator();
        while (it.hasNext()) {
            VideoBean videoBean = (VideoBean) it.next();
            if (videoBean.getEditState() == 2) {
                reqCollect.getVideoIds().add(Integer.valueOf(videoBean.getVideoId()));
                it.remove();
            }
        }
        for (int i = 0; i < this.adapter.getData().size(); i++) {
            ((VideoBean) this.adapter.getData().get(i)).setEditState(0);
        }
        this.adapter.notifyDataSetChanged();
        this.model.deleteCollect(new Gson().toJson(reqCollect));
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void deleteSingleCollect(int i) {
        ReqCollect reqCollect = new ReqCollect();
        reqCollect.getVideoIds().add(Integer.valueOf(((VideoBean) this.adapter.getData().get(i)).getVideoId()));
        this.adapter.getData().remove(this.adapter.getData().get(i));
        this.adapter.notifyDataSetChanged();
        this.model.deleteCollect(new Gson().toJson(reqCollect));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void likeVideoEvent(LikeVideoEvent likeVideoEvent) {
        if (!likeVideoEvent.isLike()) {
            Iterator it = this.adapter.getData().iterator();
            while (it.hasNext()) {
                if (likeVideoEvent.getVideoId() == ((VideoBean) it.next()).getVideoId()) {
                    it.remove();
                    this.adapter.notifyDataSetChanged();
                    return;
                }
            }
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void editCollect() {
        if (this.isEdit) {
            for (int i = 0; i < this.adapter.getData().size(); i++) {
                ((VideoBean) this.adapter.getData().get(i)).setEditState(1);
            }
            this.adapter.notifyDataSetChanged();
            return;
        }
        for (int i2 = 0; i2 < this.adapter.getData().size(); i2++) {
            ((VideoBean) this.adapter.getData().get(i2)).setEditState(0);
        }
        this.adapter.notifyDataSetChanged();
    }

    @Override // androidx.fragment.app.Fragment, com.androidx.p013lv.base.p015ui.LazyFragment
    public void onDestroyView() {
        RecyclerView.AdapterDataObserver adapterDataObserver;
        MineCollectVideoAdapter mineCollectVideoAdapter = this.adapter;
        if (!(mineCollectVideoAdapter == null || (adapterDataObserver = this.dataObserver) == null)) {
            mineCollectVideoAdapter.unregisterAdapterDataObserver(adapterDataObserver);
        }
        EventBus.getDefault().unregister(this);
        MineCollectModel mineCollectModel = this.model;
        if (mineCollectModel != null) {
            mineCollectModel.cancelHttp();
        }
        super.onDestroyView();
    }

    /* renamed from: com.androidx.lv.mine.fragment.CollectFragment$OnClick */
    public class OnClick {
        public OnClick() {
        }

        public void allSelectedBtn() {
            if (!CollectFragment.this.isOnClick()) {
                for (int i = 0; i < CollectFragment.this.adapter.getData().size(); i++) {
                    ((VideoBean) CollectFragment.this.adapter.getData().get(i)).setEditState(2);
                }
                CollectFragment.this.adapter.notifyDataSetChanged();
            }
        }

        public void deleteBtn() {
            if (!CollectFragment.this.isOnClick()) {
                CollectFragment.this.deleteCollectVideo();
                CollectFragment collectFragment = CollectFragment.this;
                collectFragment.isEdit = !collectFragment.isEdit;
                ((FragmentCollectBinding) CollectFragment.this.binding).setIsEdit(Boolean.valueOf(CollectFragment.this.isEdit));
                CollectFragment.this.model.setActionLive(CollectFragment.this.isEdit);
            }
        }
    }
}
